home *** CD-ROM | disk | FTP | other *** search
- /*
- * Image Control
- * ------------------
- * Alex Raftis
- * Sr. Project
- * CalPoly San Luis Obispo
- *
- * Version: 0.9 (alpha)
- * Last Modified: 10/10/91
- *
- * This package is a set of utilities for manipulating images stored in NXBitmapImageRep.
- * It is linked into the control object, NXBitmapImageRepControl at initialization time, so
- * you may modify it at any time. To be useful, however, it must fully implement all functions.
- * The flexibility of run time linking is added so that a user has some choice over the various
- * algorhythms used, and may replace them if need be. The object code should appear in
- * some Library directory, either ~/Library/Converters, /LocalLibrary/Converters, or
- * /NextLibrary/Converters. The libraries will be searched in that order.
- *
- * Revision History:
- * 5-25-92 I think I fixed the shearing problem for get next pixel functions. This will need
- * some more serious testing, and I have not yet fixed the get xy functions will will
- * still produce the shearing effect.
- */
-
- #ifndef __IMAGE__
- #define __IMAGE__
-
- #import <objc/Object.h>
-
- /*
- * Color Space defines. Mostly useless. You should use defines in graphics.h for
- * the varios NXColorSpaces. Also, methods to the NXBitmapImageRep help to
- * remove the need for these, as the supplied methods can give this information.
- */
- #define IMAGE_BW 1
- #define IMAGE_RGB 2
- #define IMAGE_CYMK 3
- #define IMAGE_NOALPHA 0
- #define IMAGE_ALPHA 1
-
- /*
- * Macros that give various data from the photometric interpretation field of
- * of tiff image. Mostly useless since the NXBitmapImageRep can supply this
- * information.
- */
- #define COLOR(i1) (i1 & 0x3)
- #define ALPHA(i1) ((i1 & 0x4) >> 2)
- #define PALETTE(i1) ((i1 & 0x8) >> 3)
- #define IMAGE_HASALPHA(alpha) (ALPHA(alpha) == IMAGE_ALPHA)
-
- /*
- * Macro that converts an RGB image to gray scale, of the same magnitude at the
- * original RGB values. Ie, if the max RGB values are 15, the max gray scale (white)
- * will be 15.
- */
- #define MONO(rd,gn,bl) (((rd)*11 + (gn)*16 + (bl)*5) >> 5) /*.33R+ .5G+ .17B*/
-
- typedef unsigned long pixel;
-
- /*
- * Pixel
- * ------
- * Contains the data associated with a pixel. The count is the same as samples per
- * pixels. The values in values will be the following when the following circumstance
- * are true.
- * 1. Black and White image- values[1] gray scale 1 is white depending on the current
- * color space. Message NXBitmapImageRep to be sure.
- * values[2] contains alpha, if present.
- * 2. RGB color image- values[0] = Red, values[1] = Green, values[2] = Blue.
- * values[4] = alpha, if present.
- * 3. CMYK color image- values[0] = Cyan, values[1] = Magenta, values[2] = Yellow,
- * values[3] = Black. values[4] = alpha if present.
- * The max value of each values[] is equal to 2 ^ BitsPerSample - 1. You should always
- * querry the orignal NXBitmapImageRep to be sure of the color space for the given image,
- * and thus the values of the array.
- */
- typedef struct {
- pixel values[5];
- int count;
- } Pixel;
-
- /*
- * Pointer to a function that accepts and integer (x,y) pair, and returns a Pixel structure
- * containing the pixel found and (x,y). This does not check bounds!
- */
- typedef Pixel * (*GetPixelXYFunc)(int, int);
- /*
- * Pointer to a fucntion that returns successive pixels in an image. Returns pointers to
- * a Pixel structure containing the pixel values.
- */
- typedef Pixel * (*GetPixelNextFunc)(void);
- /*
- * A pointer to a function that converts a pixel structure (pointer to) to a gray scale
- * value. This value should be from 0 to 2^BitsPerSample-1 of the original image.
- */
- typedef Pixel * (*ToGrayFunc)(Pixel *);
- /*
- * A pointer to a function that converts a pixel structure (pointer to) to a RGB pixel
- * structure. This values returned should be from 0 to 2^BitsPerSample-1 of the original image.
- */
- typedef Pixel * (*ToRGBFunc)(Pixel *);
-
- @interface ImageControl: Object
- {
- /*
- * Note: There's no class variables since c functions need access to them, and
- * objective C does not support the concept of friend fucntions. However, by
- * making locally global variables within the context of the implementation module,
- * I can simulate this effect. Not as nice of programming, but oh-well.
- */
- }
-
- /*
- * Initializes the object. This must be called before any other method.
- * Assumes: imageIn is an NXBitmapImageRep, or will at least responds to it's messages.
- * Returns: A new instance of ImageControl.
- */
- + new: (id)imageIn;
-
- /*
- * Returns a pointer to a C function call that will get a random pixel (x,y) from an NXBitmap-
- * ImageRep. The function can then be called getting passed an (x,y) coordinate pair. The
- * only limitation is that the (x,y) pair must fall with in the domain of the image. It returns
- * a pointer to a pixel structure. This structure will contain up to five values representing the
- * pixel. This can then be passed to other routines like PixelToGray.
- *
- * Assumes: Object initialized.
- * Returns: A Pointer to a fucntion of type GetPixelXYFunc.
- */
- - (GetPixelXYFunc)getXYFunction;
-
- /*
- * Resets the get next pixel function to point to the pixel in the top left hand corner of the
- * image. Note that this is different than normal postscript images which begin with (0,0)
- * in the lower left hand corner.
- *
- * Assumes: Object initialized.
- * Returns: self.
- * Results: Object ready to accept first call to GetNextFunction (see below).
- */
- - resetNext;
-
- /*
- * Returns a pointer to the correct get next function depending on the image type. This
- * function, when call after resetNext, will eventually supply all pixels in the image. It's
- * very useful for converting images. A sample algorhythms would be to:
- * 1. resetNext.
- * 2. Call routine width * height times getting a pointer to a pixel.
- * 3. Call the correct conversion routine for the type of output you will be doing (Ie,
- * RGB or BW).
- * 4. Write the correct pixel type out to the image your saving.
- *
- * Assumes: Object initialized and resetNext has been called, if you wish to start from
- * the beginning of the program. Also, the programmer must take care to run
- * past the end of the picture, or unexpected results will occur.
- * Returns: Returns a pointer to a Pixel structure.
- */
- - (GetPixelNextFunc)getNextFunction;
-
- /*
- * Returns a pointer to a function that converts pixel values to gray scale, where high
- * values represent white.
- *
- * Assumes: Object initialized.
- * Returns: Pointer to a function of type ToGrayFunc.
- */
- - (ToGrayFunc) getToGrayFunction;
-
- /*
- * Returns a pointer to a function that converts pixel values to rgb values, where high
- * values represent full intensity.
- *
- * Assumes: Object initialized.
- * Returns: Pointer to a function of type ToRGBFunc.
- */
- - (ToRGBFunc) getToRGBFunction;
-
- /*
- * Applies a Floyd Sternberg Ditherizing algorhythm to the image and return a new
- * NXBitmapImageRep of a one bit image, where 1 is white.
- *
- * Assumes: Object initialized.
- * Returns: A NXBitmapImageRep (1 = BPP, 1 = SPP, CS = NX_OneIsWhiteColorSpace.
- */
- - ditherImage;
-
- /*
- * A most knarly and nasty function, this takes the original data and conerts into a picture
- * with a palette of specified size. Currently, it's implemented via a simple algorhtym that
- * contructs a generic palette with a sample color from the full spectrum. This works well
- * on pictures with highly contrast, varied colors, but pretty much sucks other wise. It
- * get worse with the smaller the palette size. It should ideally implement a Medim-Cut
- * Algorhythm, but I couldn't find the references on a good implementation, so I just hacked
- * (read borrowed and modified) this piece of code from XV, an image view for X. It works,
- * but produces images of questionable quality. Note, however, that if the image is black
- * and white, a simple conversion is done on the data to make it matched the specified
- * palette size. Likewise, should the image contain less than the palette size in colors,
- * a simple conversion is done to preserve the original color set.
- *
- * Assumes: Object initialized. size is the size of the requested palette. It should be
- * be from 8 to 256, due to limitations of the algorhythm. outPic is
- * a pointer to a pointer that will hold the final image. Space will be allocated
- * for the image. r, g, and b are pointers to an array that can hold size bytes
- * (unsigned) char worth of data. This will envtually be the palette. This
- * should never be used to construct palettes greater than 256 colors.
- * Returns: self
- * Results: outPic contains width * height palette entries into arrays r,g, and b.
- */
- - convertToPalette: (int)size
- andReturn: (unsigned char **)outPic
- andPalettes: (unsigned char *)r : (unsigned char *)g : (unsigned char *)b;
-
- @end
-
- #endif
-